<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\RechargeOrder;
use App\Services\OrderAnalysisService;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;

class HomeController extends Controller
{
    protected $analysisService;

    public function __construct(OrderAnalysisService $orderAnalysisService)
    {
        $this->analysisService = $orderAnalysisService;
    }

    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('首页统计');
            $content->description('Description...');
            $headers = [
                '统计维度(今日)', '订单量', '交易金额', '商户所得', '代理所得', '平台总所得', '平台利润'
            ];
            //today analysis
            $data = $this->analysisService->todayDataInAdmin();
            $success_rate = @bcdiv($data['done_num'], $data['order_num'], 4) * 100;
            $table_data = [
                ['所有', $data['order_num'], $data['order_amt'], $data['mch_amt'], $data['proxy_amt'], $data['plat_all_settle'], $data['plat_all_profit']],
                ['成功', $data['done_num'] . '(成功率:'.$success_rate.'%)', $data['done_amt'], $data['mch_done_amt'] .'(已结算：'.$data['mch_done_settle'].')',
                    $data['proxy_done_amt'].'(已结算：'.$data['proxy_done_settle'].')', $data['plat_done_settle'], $data['plat_done_profit']],
            ];
            $table = new Table($headers, $table_data, ['', 'success']);


            $content->row(function (Row $row) use ($table) {
                $row->column(12, $table->render());
                $row->column(12, view('admin.charts.form'));
                $tab = new Tab();
                $tab->add('财务收支明细', view('admin.charts.finance'));
                $tab->add('订单量', view('admin.charts.order'));
                $tab->add('通道统计', view('admin.charts.payments'));
                $tab->add('接口商统计', view('admin.charts.interfaces'));
                $row->column(12, $tab->render());
            });
//
        });
    }
}
